<html>
<head></head>
<body>
<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">产品销售</h2>
        <span class="layui-breadcrumb pull-right"> <a href="#!home_console">首页</a> <a><cite>产品销售</cite></a> </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-fluid layadmin-cmdlist-fluid">
            <div class="layui-row layui-col-space30">
                <% for(s in stocks) { %>
                <div class="layui-col-md2 layui-col-sm4">
                    <div class="cmdlist-container">
                        <a href="javascript:;"> <img src="${s.product.productPic}"/> </a>
                        <div class="cmdlist-text">
                            <p>名称：${s.product.productName}</p>
                            <p>数量：${s.productAmount}</p>
                            <div class="price" product="${s.product.id}" data="${s.id}" name="${s.product.productName}">
                               
                                <button class="layui-btn layui-btn-radius layui-btn-fluid layui-btn-sm layui-btn-normal addStock"
                                        style="margin: 10px 0;">店内领用
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <% } %>
            </div>
        </div>
        <link rel="stylesheet" href="${ctxPath }/assets/css/template.css"/>

    </div>
</div>

<script>
    layui
        .use(
            ['element', 'admin', 'laypage', 'layer', 'util'],
            function () {
                var element = layui.element, $ = layui.$, admin = layui.admin, laypage = layui.laypage,
                    layer = layui.layer, util = layui.util;

                $('.addStock').click(
                    function () {
                        var stockId = $(this).parent().attr("data");
                        var productId = $(this).parent().attr("product");
                        layer.prompt(
                            {
                                formType: 0,
                                value: 0,
                                title: '确定要进行领用操作么？',
                                area: ['800px', '350px']//自定义文本域宽高
                            },
                            function (value, index, elem) {
                                if (isNaN(value)) {
                                    layer.msg("请输入数字！", {
                                        icon: 1,
                                        time: 500
                                    })
                                    return;
                                }
                                layer.load(2);
                                var parm = {
                                    type: 4,
                                    stockId: stockId,
                                    amount: value,
                                    productId: productId,
                                    shopId: $("#shopInfo").attr("data")
                                };
                                admin.req(
                                    "service/productSale/use",
                                    parm,
                                    function (
                                        data) {
                                        layer.closeAll('loading');
                                        if (data.code == 1) {
                                            layer.msg(
                                                    data.data,
                                                    {
                                                        icon: 1,
                                                        time: 1000
                                                    },
                                                    function () {
                                                        location.reload();
                                                    })
                                        } else {
                                            layer.msg(
                                                    data.data,
                                                    {
                                                        icon: 2,
                                                        time: 1000
                                                    },
                                                    function () {
                                                        location.reload();
                                                    })
                                        }

                                    },
                                    "post");

                            });
                    })

                $('.stockRecord').click(
                    function () {
                        var stockId = $(this).parent()
                            .attr("data");
                        var name = $(this).parent().attr(
                            "name");
                        var productId = $(this).parent().attr("product");
                        showDetailModel(stockId, name, productId);
                    })

                // 显示表单弹窗
                var showDetailModel = function (data, name, productId) {
                    var shopId = $("#shopInfo").attr("data");
                    admin.popupCenter({
                        area: '40%',
                        title: "购买产品--" + name,
                        path: "service/productSale/sale?shopId=" + shopId + "&stockId=" + data + "&productId=" + productId,
                        data: {
                            shopId: data
                        },
                        finish: function () {
                            location.reload();
                        }
                    });
                };

            });
</script>

</body>
</html>